package com.xzl.authserver;

import com.alibaba.druid.pool.DruidDataSource;
import com.xzl.boilerplate.common.utils.RestTemplateUtil;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;

@SpringBootApplication
@EnableEurekaClient
@ComponentScan("com.xzl")
public class AuthServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthServerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplateUtil restTemplateUtil() {
        RestTemplateUtil restTemplateUtil = new RestTemplateUtil(new RestTemplate());
        return restTemplateUtil;
    }

//	@Bean
//	public DataSource activitiDataSource() {
//		DruidDataSource dataSource = new DruidDataSource();
//		//设置数据源的属性
//		setDruidProperties(dataSource);
//		return dataSource;
//	}

    //	//设置数据源的属性的方法
//	private void setDruidProperties(DruidDataSource dataSource) {
//		dataSource.setUrl("jdbc:mysql://localhost:3306/auth_server?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false");
//		dataSource.setUsername("root");
//		dataSource.setPassword("root");
//		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//		dataSource.setMaxActive(50);
//		dataSource.setInitialSize(20);
//		dataSource.setMinIdle(8);
//	}
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                //Due to CONFIDENTIAL and /*, this will cause Tomcat to redirect every request to HTTPS.
                //You can configure multiple patterns and multiple constraints if you need more control over what is and is not redirected.
                int a = 1;
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;

    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

        //Set the scheme that will be assigned to requests received through this connector
        //@param scheme The new scheme
        connector.setScheme("http");

        //Set the port number on which we listen for requests.
        // @param port The new port number
        connector.setPort(80);

        //Set the secure connection flag that will be assigned to requests received through this connector.
        //@param secure The new secure connection flag
        //if connector.setSecure(true),the http use the http and https use the https;else if connector.setSecure(false),the http redirect to https;
        connector.setSecure(false);

        //redirectPort The redirect port number (non-SSL to SSL)
        connector.setRedirectPort(8091);
        return connector;
    }
}
